package cn.aws360.boot.extend.open.biz.service.impl;

import cn.aws360.boot.extend.open.api.constant.enums.HotsApiEnum;
import cn.aws360.boot.extend.open.api.pojo.response.ItemModel;
import cn.aws360.boot.extend.open.biz.helper.ApiHelper;
import cn.aws360.boot.extend.open.biz.service.HotsService;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author junnan
 */
@Service
@RequiredArgsConstructor
public class HotsServiceImpl implements HotsService {

    @Override
    public List<ItemModel> getNewsList(String type) {
        HotsApiEnum apiEnum = HotsApiEnum.getEnum(type);
        if (ObjectUtil.isNotNull(apiEnum)) {
            JSONObject resultJson = ApiHelper.getApiResult4Han(apiEnum.getUrl());
            if (ObjectUtil.isNotNull(resultJson)) {
                JSONArray dataArr = resultJson.getJSONArray("data");
                if (ObjectUtil.isNotNull(dataArr) && dataArr.size() > 0) {
                    List<ItemModel> itemModelList = new ArrayList<>();
                    for (Object o : dataArr) {
                        JSONObject json = (JSONObject) o;
                        itemModelList.add(ItemModel.builder()
                                .url(json.getString("url"))
                                .pic(json.getString("pic"))
                                .title(json.getString("title"))
                                .summary(json.getString("desc"))
                                .hotValue(json.getString("hot")).build());
                    }
                    return itemModelList;
                }
            }
        }
        return null;
    }

}
